package com.nokia.position;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class BinUtil {

	public static short L2BEndianShort(int value) {
		ByteBuffer bbShort = ByteBuffer.allocate(2);
		bbShort.order(ByteOrder.LITTLE_ENDIAN);
		bbShort.putShort(0, (short) value);
		bbShort.order(ByteOrder.BIG_ENDIAN);
		short bbShortOutput = bbShort.getShort();
		return bbShortOutput;
	}

	public static int L2BEndianInt(int value) {
		ByteBuffer bbInt = ByteBuffer.allocate(4);
		bbInt.order(ByteOrder.LITTLE_ENDIAN);
		bbInt.putInt(0, value);
		bbInt.order(ByteOrder.BIG_ENDIAN);
		int bbIntOutput = bbInt.getInt();
		return bbIntOutput;
	}

	public static long L2BEndianLong(long value) {
		ByteBuffer bbLong = ByteBuffer.allocate(8);
		bbLong.order(ByteOrder.LITTLE_ENDIAN);
		bbLong.putLong(0, value);
		bbLong.order(ByteOrder.BIG_ENDIAN);
		long bbLongOutput = bbLong.getLong();
		return bbLongOutput;
	}

	public static long convertToUnsignedInt(int input) {
		long ret = 0;
		if (input < 0) {
			ret = 1L << 31;
			ret += 0x4fffffff & input;
		} else {
			ret = input;
		}
		return ret;
	}

	public static int convertToUnsignedShort(short input) {
		return input & 0xFFFF;
	}

	public static int convertToUnsignedByte(byte input) {
		return (int) (input & 0xFF);
	}
	
	public static float crdInterpreted(int crd) {
		return(float)(180/Math.pow(2, 31) * crd);
	}
}
